//
//  CZNewWorkTools.swift
//  微博
//
//  Created by 霸气的刘吉庆 on 16/4/19.
//  Copyright © 2016年 ios. All rights reserved.
//

import UIKit
import AFNetworking


//定义枚举

enum CZRequestMethod: String{
    
    case Post = "post"
    
    case Get = "get"
    
    
}

class CZNewWorkTools: AFHTTPSessionManager {
    
    //首先创建一个工具类,因为很多地方都需要用到它
    
    
    //设置单利,全局访问
    
    static let sharedTools: CZNewWorkTools = {
        
       
        let tools = CZNewWorkTools()
        
        //添加可支持的反序列化器
        
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        
        return tools
        
        
    }()
    
    //上传的方法的代码的抽取
    
    //一般上传文件我们使用的都是post方法,所有这里可以不用写方法
    func upload(urlString: String,paramenters: AnyObject?,Datas: [String: NSData],finished:CZRequestCallBack){
        
        
       self.POST(urlString, parameters: paramenters, constructingBodyWithBlock: { (formData) -> Void in
            
            //拼装需要上传的数据
            for (key,value) in Datas{
                
                
                formData.appendPartWithFileData(value, name: key, fileName: "aa.png", mimeType: "application/octet-stream")
                
            }
            
            }, progress: nil, success: { (_, responseObject) -> Void in
                
                finished(responseObject: responseObject, error: nil)
                
                
                
            }) { (_, error) -> Void in
                
                finished(responseObject: nil, error: error)
                
                
        }
        
        
        
    }
    
    
    
    //定义请求
    
    typealias CZRequestCallBack = (responseObject: AnyObject?,error: NSError?)->()
    
    
        func request(method: CZRequestMethod = .Get,urlString: String,paramenters: AnyObject?,finished:CZRequestCallBack){
            
            
//            GET(<#T##URLString: String##String#>, parameters: <#T##AnyObject?#>, progress: <#T##((NSProgress) -> Void)?##((NSProgress) -> Void)?##(NSProgress) -> Void#>, success: { (NSURLSessionDataTask, AnyObject?) -> Void in
//                <#code#>
//                }) { (NSURLSessionDataTask?, NSError) -> Void in
//                    <#code#>
//            }
           
            let successs = {(dataTasks: NSURLSessionDataTask?,responseObject: AnyObject?) -> Void in
                
                finished(responseObject: responseObject, error: nil)
                
                
                
            }
            
            let failure = {(dataTasks: NSURLSessionDataTask?,error: NSError?) -> Void in
                
                
                finished(responseObject: nil, error: error)
                
                
                
                
                
            }
            
            
            if method == .Get{
                
                GET(urlString, parameters: paramenters, progress: nil, success: successs, failure: failure)
                
            }else{
                
                
                POST(urlString, parameters: paramenters, progress: nil, success: successs, failure: failure)
            }
            
            
            
            
    
    
    }
    //创建get 和 post的方法
    
//    func request(method: CZRequestMethod,urlString: String,paramenters: AnyObject?,finished:(responseObject: AnyObject?, error: NSError?)->()){
//        
//        //进行判断
//        
//        if method == .Get {
//            
//            
//            
//            GET(urlString, parameters: nil, progress: nil, success: { (_, responseObject) -> Void in
//                
//                finished(responseObject: responseObject, error: nil)
//                
//                
//                }, failure: { (_, error) -> Void in
//                    
//                    
//                    finished(responseObject: nil, error: error)
//            })
//            
//            
//            
//        }else{
//            
//          
//            POST(urlString, parameters: nil, progress: nil, success: { (_, responseObject) -> Void in
//                finished(responseObject: responseObject, error: nil)
//                
//                
//                }, failure: { (_, error) -> Void in
//                    
//                    finished(responseObject: nil, error: error)
//                    
//            })
//            
//            
//            
//            
//            
//        }
//        
//        
//        
//        
//        
//        
//        
//        
//        
//        
//        
//        
//    }
    
    
    
    
    
    
    
    

}
